package com.info.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.UUID;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.info.biz.ExcelBiz;
import com.info.data.GlobalData;

public class ExcelExport extends HttpServlet {

	public  File file =null;
	private ExcelBiz excelBiz=new ExcelBiz();
	/**
	 * Constructor of the object.
	 */
	public ExcelExport() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	  
	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
		
	}
	
	 public void downLoad(HttpServletResponse response, String headName, String fileUrl) throws Exception{
	        
//       Logger.log("begin download " + headName);        
       if (headName.endsWith(".jpg") || (headName.endsWith(".gif"))) {
           //no need setting content type and header
       } else if (headName.endsWith(".txt")) {
           response.setContentType( "text;charset=GB2312");
           response.setHeader("Content-disposition",  
                             "attachment; filename=" + headName );
       } else {
           response.setContentType( "application/doc;charset=GB2312");
           response.setHeader("Content-disposition",  
                             "attachment; filename=" + headName );
       }

       String fileURL = fileUrl;
//       Logger.log(fileURL);
       if(fileURL == null)
           return;
       
       try{
           File file = new File(fileUrl);            
           FileInputStream bis = new FileInputStream(file);            
           OutputStream bos = response.getOutputStream(); 

           byte[] buff = new byte[1024];
           int readCount = 0;
           int i = 0;
           readCount = bis.read(buff);
           while (readCount != -1){
              bos.write(buff, 0, readCount); 
              readCount = bis.read(buff);
           }
//           Logger.log("read finished!");
           if (bis!=null)
               bis.close();            
           if (bos!=null)
               bos.close();
           if(file!=null){
        	   System.out.println(" *********   "+file.delete()+" **");
           }
//           System.out.println(" ********* file.notify()*******");
       }catch(Exception e){
          e.printStackTrace();           
          throw e;
       }
   }

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {

		String searchName=request.getParameter("searchName");
		if(searchName==null && "".equals(searchName))
			return;
		try {
			UUID uuid = UUID.randomUUID();
			String tempName=uuid.toString().replace("-", "")+".xls";
			String path =GlobalData.upFilePath;
			String filePath=path +File.separator+tempName;
			
			
			
			excelBiz.exportIndicatorData(filePath, searchName,response);
//			downLoad(response,tempName,filePath);
//			RequestDispatcher dispatcher = request.getRequestDispatcher("Indicator_Upload/"+tempName);
//			dispatcher.forward(request, response);  
			
//			file = new File(path +File.separator+tempName);
//			try {
//				Thread.sleep(60*1000L);
//				System.out.println("=====================:"+path +File.separator+tempName);
//				
////				boolean isDelete=file.delete();
////				System.out.println(" ================:"+isDelete);
//			} catch (Exception e) {
//				// TODO: handle exception
//				e.printStackTrace();
//			}
//			response.sendRedirect("servlet/DeleteFile?filePath="+path +File.separator+tempName);
//			request.getRequestDispatcher("servlet/DeleteFile?filePath"+path +File.separator+tempName).forward(request, response);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServletException(e); 
		}
}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
